package com.leo.erp.web.userbusiness;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.leo.erp.dto.UserBusinessExecution;
import com.leo.erp.entity.UserBusiness;
import com.leo.erp.enums.UserBusinessStateEnum;
import com.leo.erp.service.UserBusinessService;
import com.leo.erp.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/userbusiness")
public class UserBusinessManagementController {
	@Autowired
	private UserBusinessService userBusinessService;
	
	@RequestMapping(value = "/updateBtnStr")
	@ResponseBody
	public Map<String, Object> updateBtnStr(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int userBusinessId = HttpServletRequestUtil.getInt(request, "userBusinessId");
		String btnStr = HttpServletRequestUtil.getString(request, "btnStr");
		UserBusinessExecution ue = userBusinessService.updateBtnStr(userBusinessId, btnStr);
		if(ue.getState() == UserBusinessStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
			modelMap.put("msg", "成功");
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询权限失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/getBasicData")
	@ResponseBody
	public Map<String, Object> getBasicData(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String keyId = HttpServletRequestUtil.getString(request, "KeyId");
		String type = HttpServletRequestUtil.getString(request, "Type");
		
		UserBusinessExecution ue = userBusinessService.getBasicData(keyId, type);
		if(ue.getState() == UserBusinessStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
			modelMap.put("userBusinessList", ue.getUserBusinessList());
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询权限失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/checkIsValueExist")
	@ResponseBody
	public Map<String, Object> checkIsValueExist(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String type = HttpServletRequestUtil.getString(request, "type");
		String keyId = HttpServletRequestUtil.getString(request, "keyId");
		
		UserBusinessExecution ue = userBusinessService.checkIsValueExist(type, keyId);
		if(ue.getState() == UserBusinessStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
			modelMap.put("id", ue.getUserBusiness().getUserBusinessId());
		}else {
			modelMap.put("success", false);
			modelMap.put("id", null);
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/update")
	@ResponseBody
	public Map<String, Object> update(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int userBusinessId = HttpServletRequestUtil.getInt(request, "id");
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		
		UserBusiness userBusiness = JSON.parseObject(beanJson, UserBusiness.class);
		userBusiness.setUserBusinessId(userBusinessId);
		
		UserBusinessExecution ue = userBusinessService.updateUserBusiness(userBusiness);
		if(ue.getState() == UserBusinessStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "修改失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, Object> add(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		
		UserBusinessExecution ue = userBusinessService.insertUserBusiness(beanJson, request);
		if(ue.getState() == UserBusinessStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "添加信息失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/getuserbusinesslist")
	@ResponseBody
	public Map<String, Object> getUserBusinessList(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		
		//获取前台传过来的值
		String type = HttpServletRequestUtil.getString(request, "type");
		String keyId = HttpServletRequestUtil.getString(request, "keyId");
		String deleteFlag = HttpServletRequestUtil.getString(request, "deleteFlag");
		
		List<UserBusiness> userBusinessList = userBusinessService.getUserBusinessList(type, keyId, deleteFlag);
		
		if(userBusinessList.size() > -1) {
			modelMap.put("count", userBusinessList.size());
			modelMap.put("userBusinessList", userBusinessList);
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询数据失败");
		}
		
		return modelMap;
	}
}
